#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import ConfigParser
#import xmllangs
import sys

debug = True

#LANGSXML_PATH=sys.path[0]+"/langs.xml"

class boxConfig:
	
	section = "options"

	files = [os.path.expanduser( "~/.suindara.conf" ), 
			sys.path[0]+"/suindara.conf"]

	actual_file = os.path.expanduser( "~/.suindara.conf" )

	"""
		Configuration abstraction layer
		boxGUI will use following functions to handle configuration
		thus will not directly manipulate configuration dict
	"""
	options = {}

	"""options = { "boxlogin":None, 
				"boxpassword":None, 
				"browserpath":"firefox", 
				"lang":"English", 
				"savelogin":0 ,
				"startuplogin":None
				}
	login = {	"login": options["startuplogin"],
				"boxpassword": None,
				"savelogin":0
				}"""
				
	def load_config(self):
		"""
			Here we will be loading the configuration file to a dict
			dict being: options = {'section': []}
			[] list aquires the values in conf file (same order)
			if there is no options section in config file, it is created in dict
			and subsequently saved to conf file on exit
		"""
		config = ConfigParser.ConfigParser()
		config.read( self.files[0] )
		c = 0 # var for checking if options sections exists
		for section in config.sections():
			if section == 'options': c = 1
			if debug: print section
			if section == 'options':
				tmpData = [None,None,None,None,None,None,None,None]				
				for option in config.options(section):
					if debug: print " ", option, "=", config.get(section, option)
					if option == 'language': tmpData[0] = config.get(section, option)
					elif option == 'startuplogin': tmpData[1] = config.get(section, option)
					elif option == 'browserpath': tmpData[2] = config.get(section, option)
					elif option == 'autoconnect': tmpData[3] = int(config.get(section, option))
					elif option == 'publiccomputer': tmpData[4] = int(config.get(section, option))
					elif option == 'secureconnection': tmpData[5] = int(config.get(section, option))
					elif option == 'downloadsfolder': tmpData[6] = config.get(section, option)
					elif option == 'downloadsoverwrite': tmpData[7] = int(config.get(section, option))
				self.options['options'] = tmpData
			else:
				tmpData = []				
				for option in config.options(section):
					if debug: print " ", option, "=", config.get(section, option)
					tmpData.append(config.get(section, option))
				self.options[section] = tmpData				
		if c == 0:
			a = []
			a.append('english'), a.append('None'), a.append('firefox'), a.append(0), a.append(0), a.append(1), a.append(os.path.expanduser( "~/Suindara downloads" )), a.append(1)
			self.options['options'] = a
		if debug: print self.options
				
	def get_option(self, section, key):
		return self.options[section][key]

	def get_accounts(self):
		a = []
		for k in self.options.keys():
			if k != "options":
				a.append(k)
		return a

	def get_int_accounts(self):
		a = 0
		for k in self.options.keys():
			if k != "options":
				a+=1
		return a
		
	def get_int_option(self, section, key):
		return self.options[section][key]
		
	def set_option(self, section, key, value):
		if( value!=None and value!="" and section != 'options'):
			try:
				a = self.options[section]
			except:
				a = [section, '', 0]
			a[key] = value
			self.options[section] = a
			if debug: print self.options
			
	def get_goption(self, key):
		a = self.options['options']
		return a[key]
			
	def set_goption(self, key, value):
		a = self.options['options']
		a[key] = value
		self.options['options'] = a
				
	def remove_option(self, section, key):
		self.options[section][key] = None
		
	def save_config(self):
		if debug: print '* options are: ' + str(self.options['options'])
		config = ConfigParser.ConfigParser()
		config.add_section('options')
		s = 1
		for r in self.options['options']:
			if debug: print '* r for %s is %s' % (s, r)
			if r == '': r = 'None'
			if s == 1: config.set('options', 'language', r)
			if s == 2: config.set('options', 'startuplogin', r)
			if s == 3: config.set('options', 'browserpath', r)
			if s == 4: config.set('options', 'autoconnect', r)
			if s == 5: config.set('options', 'publiccomputer', r)
			if s == 6: config.set('options', 'secureconnection', r)
			if s == 7: config.set('options', 'downloadsfolder', r)
			if s == 8: config.set('options', 'downloadsoverwrite', r)
			s+=1
		if self.get_goption(4) == 0:
			for k in self.get_accounts():
				config.add_section(k)
				s = 1
				for r in self.options[k]:
					if r == '': r = 'None'
					if s == 1: config.set(k, 'login', r)
					if s == 2: config.set(k, 'password', r)
					if s == 3: config.set(k, 'savelogin', r) 
					s+=1
		config.write(open(self.files[0], "w"))

#	def save_config(self):
#		config = ConfigParser.RawConfigParser()
#		config.add_section("options")
#		for item in self.options:
#			if( self.options[item]!=None):
#				config.set(self.section, item,
#						    self.options[item])
#		config.write(open(self.actual_file,"w"))
#
